summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/uic/barcode/staticFrame/DataRecord.java
blob: 16d38111b65281bd828e25513f6798e95f06a855 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package org.uic.barcode.staticFrame;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;

import org.uic.barcode.ticket.EncodingFormatException;

/**
 * The Class DataRecord implements the basic decoding and encoding 
 * of the data record structure, the split into tag, version, length and content.
 * 
 * Implementing classes must provide decoding and encoding of the content
 * 
 */
public abstract class DataRecord {
	
	/** The id tag. */
	protected String idTag; 
	
	/** The version id. */
	protected String versionId;
	
	/** The content. */
	protected byte[] content;

	/**
	 * Instantiates a new data record.
	 *
	 * @param idTag the id tag
	 * @param version the version
	 */
	public DataRecord (String idTag, String version) {
		this.idTag = idTag;
		this.versionId = version;
	}
	
	/**
	 * Instantiates a new data record.
	 *
	 * @param idTag the id tag
	 */
	public DataRecord (String idTag) {
		this.idTag = idTag;
	}
	
	/**
	 * Encode.
	 *
	 * @return the byte[]
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @throws EncodingFormatException the encoding format exception
	 */
	public byte[] encode() throws IOException, EncodingFormatException {
			
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		
		encodeContent();
		
		//size of tag + version + length
		int length = 12;
			
		//size of data
		length = length + content.length;
			
		String lengthElement = String.format("%04d",length);		
		
		while (idTag.length() < 6) {
			idTag = idTag + " ";
		}
		
		while (versionId.length() < 2) {
			versionId = "0" + versionId;
		}

		outputStream.write(idTag.getBytes());
		
		outputStream.write(versionId.getBytes());
		
		outputStream.write(lengthElement.getBytes());	
		
		outputStream.write(content);				
									
		return outputStream.toByteArray();
	}
	
	/**
	 * Decode.
	 *
	 * @param byteData the byte data
	 * @return the int
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @throws EncodingFormatException the encoding format exception
	 */
	public int decode(byte[] byteData) throws IOException, EncodingFormatException {
		
	    int offset = 0;
		String tag = new String(Arrays.copyOfRange(byteData, offset,  offset + 6));
		this.setIdTag(tag);
		offset = offset + 6;
		
		String version =  new String(Arrays.copyOfRange(byteData, offset,  offset + 2));
		this.setVersionId(version);
		offset = offset + 2;				
		
		String dataSize =  new String(Arrays.copyOfRange(byteData, offset,  offset + 4));
		offset = offset + 4;			
		
		int length = Integer.parseInt(dataSize) - 12;
		this.setData(Arrays.copyOfRange(byteData, offset,  offset + length));
		
		decodeContent();
		
		return length + 12;
	}


	/**
	 * Gets the id tag.
	 *
	 * @return the id tag
	 */
	public String getIdTag() {
		return idTag;
	}


	/**
	 * Sets the id tag.
	 *
	 * @param idTag the new id tag
	 */
	public void setIdTag(String idTag) {
		this.idTag = idTag;
	}


	/**
	 * Gets the version id.
	 *
	 * @return the version id
	 */
	public String getVersionId() {
		return versionId;
	}


	/**
	 * Sets the version id.
	 *
	 * @param versionId the new version id
	 */
	public void setVersionId(String versionId) {
		this.versionId = versionId;
	}


	/**
	 * Gets the data.
	 *
	 * @return the data
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @throws EncodingFormatException the encoding format exception
	 */
	protected byte[] getData() throws IOException, EncodingFormatException {
		return content;
	}


	/**
	 * Sets the data.
	 *
	 * @param data the new data
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @throws EncodingFormatException the encoding format exception
	 */
	protected void setData(byte[] data) throws IOException, EncodingFormatException  {
		this.content = data;
	}


	
	/**
	 * Decode content.
	 *
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @throws EncodingFormatException the encoding format exception
	 */
	protected abstract void decodeContent() throws IOException, EncodingFormatException;
	
	/**
	 * Encode content.
	 *
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @throws EncodingFormatException the encoding format exception
	 */
	protected abstract void encodeContent() throws IOException, EncodingFormatException;
	
}